<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车辆品牌" prop="vehicleName">
        <el-input
            v-model="queryParams.vehicleName"
            placeholder="请输入车辆品牌"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="licenseNo">
        <el-input
            v-model="queryParams.licenseNo"
            placeholder="请输入车牌号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属商户" prop="deptName">
        <el-input
            v-model="queryParams.deptName"
            placeholder="请输入所属商户"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经营范围" prop="routeCode">
        <el-input
            v-model="queryParams.routeCode"
            placeholder="请输入经营范围"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="自编号" prop="selfNo">
        <el-input
            v-model="queryParams.selfNo"
            placeholder="请输入自编号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="锁标识" prop="lockFlag">
        <el-input
            v-model="queryParams.lockFlag"
            placeholder="请输入锁标识"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--            type="primary"-->
<!--            plain-->
<!--            icon="Plus"-->
<!--            @click="handleAdd"-->
<!--            v-hasPermi="['vehicle:info:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--            type="success"-->
<!--            plain-->
<!--            icon="Edit"-->
<!--            :disabled="single"-->
<!--            @click="handleUpdate"-->
<!--            v-hasPermi="['vehicle:info:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="multiple"
            @click="handleUpdates"
            v-hasPermi="['vehicle:info:edit']"
        >批量挪车</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--            type="danger"-->
<!--            plain-->
<!--            icon="Delete"-->
<!--            :disabled="multiple"-->
<!--            @click="handleDelete"-->
<!--            v-hasPermi="['vehicle:info:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['vehicle:info:export']"
        >导出挪车记录</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车辆编码" align="center" prop="vehicleCode" />
      <el-table-column label="车辆品牌" align="center" prop="vehicleName" />
      <el-table-column label="车牌号" align="center" prop="licenseNo" />
      <el-table-column label="车牌颜色" align="center" prop="licenseColor" />
      <el-table-column label="所属商户" align="center" prop="deptName" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="chelaing_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="营运编号" align="center" prop="certificateNo" />
      <el-table-column label="营运有效期" align="center" prop="certificateExpiryDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.certificateExpiryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="经营范围" align="center" prop="routeCode" />
      <!--      <el-table-column label="办卡日期" align="center" prop="makeCardTime" width="180">-->
      <!--        <template #default="scope">-->
      <!--          <span>{{ parseTime(scope.row.makeCardTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="办卡人" align="center" prop="makeCardUser" />-->
      <!--      <el-table-column label="自编号" align="center" prop="selfNo" />-->
      <el-table-column label="锁标识" align="center" prop="lockFlag">
        <template #default="scope">
          <dict-tag :options="sys_notice_status" :value="scope.row.lockFlag" />
        </template>
      </el-table-column>
      <el-table-column label="出厂日期" align="center" prop="productionDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.productionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="产品名称" align="center" prop="productionName" />-->
      <!--      <el-table-column label="有效期" align="center" prop="workExpiryDate" width="180">-->
      <!--        <template #default="scope">-->
      <!--          <span>{{ parseTime(scope.row.workExpiryDate, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="工作状态" align="center" prop="workStatus" >
        <template #default="scope">
          <dict-tag :options="work_status" :value="scope.row.workStatus" />
        </template>
      </el-table-column>
      <el-table-column label="电量" align="center" prop="battery" />
      <el-table-column label="使用用户" align="center" prop="workUser" />
      <el-table-column label="地址" align="center" prop="address" />
      <!--      <el-table-column label="部门ID" align="center" prop="deptId" />-->
      <!--      <el-table-column label="备注" align="center" prop="remark" />-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template #default="scope">-->
<!--          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['vehicle:info:edit']">修改</el-button>-->
<!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['vehicle:info:remove']">删除</el-button>-->
<!--          <el-button link type="primary" icon="Delete" @click="handleUpdates(scope.row)" v-hasPermi="['vehicle:info:remove']">批量挪车</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改车辆信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="infoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车辆编码" prop="siteInfo" >
          <el-select v-model="form.siteInfo" placeholder="请选择" @change="handleSiteInfo">
            <el-option
                v-for="item in options"
                :key="item.siteId"
                :label="item.siteName"
                :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目的地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入车辆品牌" />
        </el-form-item>
        <el-form-item label="位置坐标" prop="point">
          <el-input v-model="form.point" placeholder="默认位置坐标" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Info">
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/vehicle/info";
import { ref, reactive } from "vue";
import { listInfo as siteListInfo } from "@/api/site/info";
const { proxy } = getCurrentInstance();
const { sys_notice_status, chelaing_status, work_status } = proxy.useDict("sys_notice_status", "chelaing_status", "work_status");
const infoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const value = ref("");
const options = ref([]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    vehicleName: null,
    licenseNo: null,
    vehicleType: null,
    motorType: null,
    vehicleLevel: null,
    licenseColor: null,
    deptName: null,
    status: null,
    certificateNo: null,
    certificateExpiryDate: null,
    routeCode: null,
    makeCardTime: null,
    makeCardUser: null,
    selfNo: null,
    lockFlag: null,
    productionDate: null,
    productionName: null,
    workExpiryDate: null,
    workStatus: null,
    workUser: null,
    res: null,
    deptId: null,
  },
  rules: {
    licenseNo: [
      { required: true, message: "车牌号不能为空", trigger: "blur" }
    ],
    vehicleType: [
      { required: true, message: "车型不能为空", trigger: "change" }
    ],
    status: [
      { required: true, message: "状态(0启用，1禁用）不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询车辆信息列表 */
function getList() {
  loading.value = true;
  listInfo(queryParams.value).then(response => {
    infoList.value = response.rows;
    loading.value = false;
  });
}
/** 查询车辆信息列表 */
function getSiteList() {
  siteListInfo().then(response => {
    options.value = response.rows;
  });
}

function handleSiteInfo(){
  console.log("111111111111111111");
  console.log(form.value.siteInfo);
  if (form.value.siteInfo){
    form.value.address = form.value.siteInfo.siteAddress;
    form.value.point = form.value.siteInfo.sitePoint;
  }
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    vehicleCode: null,
    vehicleName: null,
    licenseNo: null,
    vehicleType: null,
    motorType: null,
    vehicleLevel: null,
    licenseColor: null,
    deptName: null,
    status: null,
    certificateNo: null,
    certificateExpiryDate: null,
    routeCode: null,
    makeCardTime: null,
    makeCardUser: null,
    selfNo: null,
    lockFlag: null,
    productionDate: null,
    productionName: null,
    workExpiryDate: null,
    workStatus: null,
    workUser: null,
    res: null,
    deptId: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("infoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.vehicleCode);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加车辆信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _vehicleCode = row.vehicleCode || ids.value
  getInfo(_vehicleCode).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改车辆信息";
  });
}

/** 修改按钮操作 */
function handleUpdates(row) {
  reset();
  open.value = true;
  title.value = "批量挪车";
  const _vehicleCodes = row.vehicleCode || ids.value;
  if (_vehicleCodes){
    form.value.vehicleCodes = _vehicleCodes;
  }

}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["infoRef"].validate(valid => {
    console.log(form.value.vehicleCodes)
    if (valid) {
      if (form.value.vehicleCodes != null) {
        updateInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _vehicleCodes = row.vehicleCode || ids.value;
  proxy.$modal.confirm('是否确认删除车辆信息编号为"' + _vehicleCodes + '"的数据项？').then(function() {
    return delInfo(_vehicleCodes);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('vehicle/info/export', {
    ...queryParams.value
  }, `info_${new Date().getTime()}.xlsx`)
}
getSiteList();
getList();
</script>
